package com.prd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.mapper.EntMeetRecordMapper;
import com.prd.model.EntMeetRecord;
import com.prd.model.LoginUser;
import com.prd.service.EntMeetRecordService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntMeetRecordServiceImpl extends ServiceImpl<EntMeetRecordMapper, EntMeetRecord> implements EntMeetRecordService {

    @Autowired
    private EntMeetRecordMapper entMeetRecordMapper;

    @Override
    public int insertInfo(EntMeetRecord entMeetRecord) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        entMeetRecord.setStatus(1);
        entMeetRecord.setPerform(1);
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entMeetRecord);
        return entMeetRecordMapper.insert(entMeetRecord);
    }

    @Override
    public int updateInfo(EntMeetRecord entMeetRecord) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entMeetRecord);
        return entMeetRecordMapper.updateById(entMeetRecord);
    }

    @Override
    public int checkRecord(Long recordId) {
        EntMeetRecord entMeetRecord = entMeetRecordMapper.selectById(recordId);
        if (entMeetRecord == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entMeetRecord.setStatus(CheckStatus.TWO.getCode());
        return updateInfo(entMeetRecord);
    }

    @Override
    public int updateRecord(EntMeetRecord entMeetRecord) {
        EntMeetRecord meetRecord = entMeetRecordMapper.selectById(entMeetRecord.getId());
        if (meetRecord == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entMeetRecord, meetRecord);
        return updateInfo(meetRecord);
    }

    @Override
    public int executeRecord(Long id) {
        EntMeetRecord meetRecord = entMeetRecordMapper.selectById(id);
        if (meetRecord == null) {
            throw new RuntimeException("记录没有找到！");
        }
        meetRecord.setPerform(2);
        return updateInfo(meetRecord);
    }
}
